﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net;

namespace gTray
{
    public partial class frmOptions : Form
    {
        public frmOptions()
        {
            InitializeComponent();

            LoadOptions();
        }

        private void LoadOptions()
        {
            //General Options
            updownTimeout.Value = (SettingService.Instance.RecheckIntervalSeconds / 60);
            updownNotificationTimeout.Value = SettingService.Instance.NotificationIntervalSeconds;
            cbAutoStart.Checked = SettingService.Instance.RunAtStartup;
        }

        private void btnGeneralDefaults_Click(object sender, EventArgs e)
        {
            updownTimeout.Value = (SettingService.RecheckIntervalDefault / 60);
            updownNotificationTimeout.Value = SettingService.NotificationIntervalDefault;

        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (FormChanges())
            {
                SettingService.Instance.RecheckIntervalSeconds = (int)(updownTimeout.Value * 60);
                SettingService.Instance.NotificationIntervalSeconds = (int)updownNotificationTimeout.Value;

                SettingService.Instance.RunAtStartup = cbAutoStart.Checked;

                SettingService.Instance.Save();
            }

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Toggles the OK button to say apply if there is any changes on the form.
        /// </summary>
        private void ToggleOKApplyButton()
        {
            if(FormChanges())
                btnOk.Text = "Apply";
            else
                btnOk.Text = "Ok";
        }

        private bool FormChanges()
        {
            return SettingService.Instance.RecheckIntervalSeconds != (int)(updownTimeout.Value * 60) ||
                SettingService.Instance.NotificationIntervalSeconds != (int)updownNotificationTimeout.Value ||
                SettingService.Instance.RunAtStartup != cbAutoStart.Checked;
        }


        #region Value Changed Events
        private void updownTimeout_ValueChanged(object sender, EventArgs e)
        {
            ToggleOKApplyButton();
        }

        private void updownNotificationTimeout_ValueChanged(object sender, EventArgs e)
        {
            ToggleOKApplyButton();
        }

        private void cbAutoStart_CheckedChanged(object sender, EventArgs e)
        {
            ToggleOKApplyButton();
        }
        #endregion
    }
}
